package com.hrt.demo.czd.service.impls;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.demo.czd.bean.User;
import com.hrt.demo.czd.dao.AddressDao;
import com.hrt.demo.czd.dao.UserDao;
import com.hrt.demo.czd.service.AddressService;
import com.hrt.demo.czd.service.UserService;
@Service
public class UserServiceImpls implements UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private AddressDao addressDao;
	@Autowired
	private AddressService addressService;
	@Override
	@Transactional
	public void saveUser(User user) throws Exception {
		userDao.saveEntity(user);

	}

	@Override
	@Transactional
	public void updateUser(User user) {
		try {
			userDao.updateEntity(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public Long totalCount() {
		return userDao.totalCount();
	}

	@Override
	public List queryByPage(int pageIndex, int pageSize) {
		return userDao.queryByPage(pageIndex, pageSize);
	}

	@Override
	public User getUserByid(Long userId) {
		// TODO Auto-generated method stub
		return userDao.getById(userId);
	}

	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<User> users=userDao.getByIds(ids);
		for(User user:users)
		{
			
			int lens = user.getAddress().size();
			Long[] addIds = new Long[lens];
			for(int i=0;i<lens;i++){
				
				addIds[i] =user.getAddress().get(i).getId();
			}
			addressService.deleteById(addIds);
			user.setIsdelete(new Integer(1));
			userDao.updateEntity(user);
		}
		
	}

}
